//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
var Isshow = false;
var Isshow1 = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 手续费
    payFlag: '',
    // 易码预知
    maskFlag: true,
    advanceFlag: true,
    // 商家资质认证
    identifyFlag: true,
    // 运营资质认证
    yunYingFlag: true,
    //
    imgUrl: baseJs.imgUrl,
    userData: [],
    myControldatafour: [
      {
        'title': '商户入驻',
        'src': '../../images/myIcon/icon8.png',
        'href': '/packageA/merchantsin/merchantsin'
      },
      {
        'title': '订单管理',
        'src': '../../images/myIcon/icon1.png',
        'href': '/packageA/ordermanagementdata/ordermanagementdata'
      },
      {
        'title': '转账记录',
        'src': '../../images/myIcon/icon17.png',
        'href': '/packageA/businessFreeOrder/businessFreeOrder'
      },
      {
        'title': '地址管理',
        'src': '../../images/myIcon/icon3.png',
        'href': '/packageA/address/address'
      }
    ],
    togetherControlData: [
      {
        'title': '转发锁定',
        'src': '../../images/myIcon/icon4.png',
        'href': ''
      },
      {
        'title': '我的邦吧',
        'src': '../../images/myIcon/icon5.png',
        // 'href': '/packageA/barOfMine/barOfMine'
      },
      {
        'title': '我的课程',
        'src': '../../images/myIcon/icon6.png',
        'href': '/packageA/course/course'
      },
      {
        'title': '我的推荐',
        'src': '../../images/myIcon/icon7.png',
        'href': '/packageA/myCollection/myCollection'
      }
    ],
    businessControlData: [
      {
        'title': '进宝柜授权',
        'src': '../../images/myIcon/icon9.png',
        'href': '/packageA/powerGive/powerGive'
      },
      {
        'title': '易码预支',
        'src': '../../images/myIcon/icon10.png',
        'href': '/packageA/advance0/advance0'
      },
      {
        'title': '店铺码',
        'src': '../../images/myIcon/icon11.png',
        'href': '/packageA/businessCode/businessCode'
      },
      {
        'title': '投诉记录',
        'src': '../../images/myIcon/tousu.png',
        'href': '/packageA/complaintDetail/complaintDetail'
      },
    ],
    cooperationData: [
      {
        'title': '商务合作',
        'src': '../../images/myIcon/icon12.png',
        'href': '/packageA/Business/Business'
      },
      {
        'title': '关于我们',
        'src': '../../images/myIcon/icon13.png',
        'href': '/packageA/aboutUs/aboutUs'
      },
      {
        'title': '专属服务人员',
        'src': '../../images/myIcon/icon14.png',
        'href': '/packageA/contactUs/contactUs'
      },
    ],
    yunYingData: [{
      'title': '月结收益',
      'src': '../../images/myIcon/icon13.png',
      'href': '/packageA/yunYingIncome/yunYingIncome'
    },
    {
      'title': '运营资质上传',
      'src': '../../images/myIcon/icon21.png',
      'href': ''
    },
    {
      'title': '仓库管理',
      'src': '../../images/myIcon/icon28.png',
      'href': '/packageA/yunYCangKu/yunYCangKu'
    },
    ],
    // 申请多收
    applyMore: true,
    apply0: 0,
    apply1: 0,
    applyMoney: "",
    checkbox: [{
      value: 1,
      des: "我明确申请多收的目的",
    },
    {
      value: 1,
      des: "我已阅读协议并同意",
    }
    ],
    uid: "",
    userType: "",
    // 手续
    givesetMealMa: '',
    allsetmealma: '',
    tapIndex: ['0', '1', '2'],
    state: '1',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var uid = '';
    var userType = '';
    userType = wx.getStorageSync('userType');
    // 更改：
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetIsServers"),
      success: function (res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var ResultData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        if (ResultData.Status == 0) {
          that.setData({
            Isshow: true,
            Isshow1: false,
            IsshowUrlString: ResultData.test4,
          })
          // console.log(IsshowUrlString);
        } else {
          that.setData({
            Isshow: false,
            Isshow1: true,
          })
          uid = wx.getStorageSync('uid');
          userType = wx.getStorageSync('userType');
          that.setData({
            uid: uid,
            userType: userType,
          })
          // console.log(userType)
          // 我的页面判断是否登录
          if (!uid && !Isshow) {
            wx.redirectTo({
              url: '/packageA/login/login',
            })
          } else {
            that.GetUserInfoByIDs();
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
    })
    this.GetMyAccountInfo();
    this.GetUserInfoByIDs();

  },
  // 营业状态
  operatingState: function () {
    wx.showLoading({
      title: '设置中',
    })
    var that = this;
    var uid = this.data.uid;
    var state = this.data.state === '1' ? '0' : '1';
    wx.request({
      url: baseJs.api_User + "BusinessSetOnlineOrOffline" + "?" + "UID=" + uid + "&t=" + state,
      success: function (res) {
        console.log(res.data)
        wx.hideLoading()
        wx.showToast({
          title: res.data.ResultMsg,
          icon: "success"
        })
        var json = res.data.ResultData;
        console.log(json);
        that.setData({
          state: state,
        })
      }
    })

  },
  // 账户升级
  bindImg: function () {
    var that = this;
    var tapIndex = this.data.tapIndex;
    var uid = this.data.uid;
    var givesetMealMa = this.data.givesetMealMa;
    var allsetmealma = this.data.allsetmealma;
    var payFlag = this.data.payFlag;
    wx.showActionSheet({
      // itemList: ['提示', '请选择账户的升级类型，审核通过后无法更改！', '申请成为商家用户', '申请成为运营中心'],
      itemList: ['提示', '请选择账户的升级类型，审核通过后无法更改！', '申请成为商家用户'],
      itemColor: '#227aff',
      success(res) {
        // console.log(res.tapIndex);
        if (res.tapIndex == 2) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=UserApplySetBusinessOrAgent&UID=" + uid + "&UserType=" + 1),
            success: function (res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              console.log(json);
              if (json.ResultFlag == 1) {
                wx.showModal({
                  title: '提示',
                  content: '已申请成为商户！',
                  showCancel: false
                })
                that.setData({
                  payFlag: true,
                  givesetMealMa: json.ResultData.GiveSetMealMa,
                  allsetmealma: json.ResultData.AllSetMealMa,
                })
                this.GetUserInfoByIDs();
              } else {
                that.setData({
                  payFlag: false,
                })
              }

            }

          })
        }
        //  if (res.tapIndex == 3) {
        //   // console.log("method=UserApplySetBusinessOrAgent&UID=" + uid + "&UserType=" + 0);
        //   wx.request({
        //     url: baseJs.api + baseJs.encryUrl("method=UserApplySetBusinessOrAgent&UID=" + uid + "&UserType=" + 0),
        //     success: function (res) {
        //       var json = JSON.parse(baseJs.decryUrl(res.data));
        //       console.log(json);
        //       if (json.ResultFlag == 1) {
        //         wx.showModal({
        //           title: '提示',
        //           content: '申请成功，请等待审核！',
        //           showCancel: false
        //         })
        //       }
        //     }
        //   })
        // }

      }
    })
  },
  // 
  personalDataTap: function () {
  },
  //获取用户信息
  GetUserInfoByIDs: function () {
    var that = this;
    var uid = this.data.uid;
    var givesetMealMa = this.data.givesetMealMa;
    var allsetmealma = this.data.allsetmealma;
    var payFlag = this.data.payFlag;

    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByIDs&UID=" + uid),
      success: function (res) {
        // console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        var BusinessAllName = json.BusinessAllName;
        var BusinessIsFinishEnter = json.BusinessIsFinishEnter;
        // console.log(json);
        that.setData({
          // payFlag: false,
          givesetMealMa: json.GiveSetMealMa,
          allsetmealma: json.AllSetMealMa,
          BusinessAllName: BusinessAllName,
          BusinessIsFinishEnter: BusinessIsFinishEnter,

        })
        wx.setStorageSync('BusinessIsFinishEnter', json.BusinessIsFinishEnter);
        wx.setStorageSync('userType', json.UserType);

      }
    })
  },
  // 招财柜
  GetMyAccountInfo: function () {
    var that = this;
    var uid = this.data.uid;
    var userData = this.data.userData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetMyAccountInfo&UID=" + uid),
      success: function (res) {
        // console.log(JSON.parse(baseJs.decryUrl(res.data)));
        userData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        that.setData({
          userData: userData,
        })
        // console.log(userData);
      }
    })
  },
  // 互动管理
  huDongTap: function (e) {
    if (e.currentTarget.dataset.index == 0 || e.currentTarget.dataset.index == 1) {
      wx.showToast({
        title: "该功能暂未开放！",
        icon: 'none',
        duration: 1000
      });
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }

  },
  // 商家控制台3
  businessControlTap: function (e) {
    var that = this;
    var uid = this.data.uid;
    var applyMoney = this.data.applyMoney;
    var index = e.currentTarget.dataset.index
    console.log(index);
    if (index == 12) {
      this.setData({
        maskFlag: false,
        advanceFlag: false,
      })
    } else if (index == 1) {
      wx.showToast({
        title: '功能暂未开放',
        icon:"none"
      })
    } else if (index == 8) {
      this.setData({
        maskFlag: false,
        identifyFlag: false,
      })
    }
    else if (index == 5) {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=GetBusinessIsMoreCome&SJID=" + uid),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          if (json.ResultFlag == 1) {
            this.setData({
              applyMore: false,
              applyMoney: json.ResultData,
            })
          }
          else {
            wx.showModal({
              title: '提示',
              content: json.ResultMsg,
              showCancel: false
            })
          }
        }
      })
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }
  },
  // advanceCancelTap
  advanceCancelTap: function () {
    this.setData({
      maskFlag: true,
      advanceFlag: true,
    })
  },
  // identifyCancelTap
  identifyCancelTap: function () {
    this.setData({
      maskFlag: true,
      identifyFlag: true,
    })
  },
  // 运营中心
  yunYingTap: function (e) {
    if (e.currentTarget.dataset.index == 1) {
      this.setData({
        maskFlag: false,
        yunYingFlag: false,
      })
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }
  },
  // yunYingCancel
  yunYingCancel: function () {
    this.setData({
      maskFlag: true,
      yunYingFlag: true,
    })
  },
  // 申请多收
  checkboxChange: function (e) {
    if (e.detail.value.length == 2) {
      this.setData({
        apply0: e.detail.value[0],
        apply1: e.detail.value[1],
      })
    } else {
      this.setData({
        apply0: 0,
        apply1: 0,
      })
    }

  },
  // applyMoreConfirm商家确认多收
  applyMoreConfirm: function () {
    var that = this;
    var uid = this.data.uid;
    var applyMoney = this.data.applyMoney;
    var apply0 = this.data.apply0;
    var apply1 = this.data.apply1;
    if (apply0 == 0 || apply1 == 0) {
      wx.showToast({
        title: "请明确申请多收的目的、阅读协议并同意",
        icon: 'none',
        duration: 3000
      });
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=BusinessSureMoreCome&SJID=" + uid + "&MoreCome=" + applyMoney),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          if (json.ResultFlag == 1) {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 3000
            });
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 3000
            });
          }
        }
      })
    }

  },
  // applyMoreCancel
  applyMoreCancel: function () {
    this.setData({
      applyMore: true
    })
  },
  // 退出登录
  clearLogin: function () {
    wx.removeStorage({
      key: "uid",
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: "退出成功！",
          icon: 'none',
          duration: 1000
        });
        setTimeout(function () {
          wx.reLaunch({
            url: '../index/index',
          })
        }, 1000)
      },
      fail: function (res) {
        console.log(res);

      },
      complete: function (res) {
        console.log(res);

      },
    });
  },
})